package com.qianfeng.xinyihezi.xinyihezi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.TagRecyclerViewAdapter;
import com.qianfeng.xinyihezi.xinyihezi.config.NetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.RetrofitInterface;
import com.qianfeng.xinyihezi.xinyihezi.jsondecode.DecodeDataTag;
import com.qianfeng.xinyihezi.xinyihezi.model.LabelList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Tcoy on 2016/11/13.
 */

public class TagModelFragment extends Fragment {
    private String TAG = getClass().getSimpleName();
    private RecyclerView mRecyclerView;
    private Retrofit mRetrofit;
    private RetrofitInterface mRetrofitInterface;
    private DecodeDataTag mDecodeDataTag;
    private TagRecyclerViewAdapter mTagRecyclerViewAdapter;
    private List<LabelList> mLabelLists;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tag_model_layout, null);
        initView(view);
        initRetrofit();
        initData();
        loadData();
        setListener();
        return view;
    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        mTagRecyclerViewAdapter.setOnClick(new TagRecyclerViewAdapter.IBackCallClick() {
            @Override
            public void onLabelId(View view) {
                Toast.makeText(getActivity(), view.getTag() + "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 初始化Retrofit
     */
    private void initRetrofit() {
        mRetrofit = new Retrofit.Builder().baseUrl(NetConfig.BASE_LEFTCLASSIFT).build();
        mRetrofitInterface = mRetrofit.create(RetrofitInterface.class);
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mRetrofitInterface.getTagJsonData().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        String jsonResult = response.body().string();
                        List<LabelList> tagDataNot = mDecodeDataTag.getTagData(jsonResult);
                        mLabelLists.addAll(tagDataNot);
                        mTagRecyclerViewAdapter.notifyDataSetChanged();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mLabelLists = new ArrayList<>();
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mDecodeDataTag = new DecodeDataTag();
        mTagRecyclerViewAdapter = new TagRecyclerViewAdapter(getActivity(), mLabelLists);
        mRecyclerView.setAdapter(mTagRecyclerViewAdapter);
    }

    /**
     * 初始化View
     */
    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewTag);
    }
}
